﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.CustomClassLibrary;
using AMM.DataAccess.Project;
using AMM.Model;
using AMM.Model.Project;

namespace AMM.Business.Project {
    public class BOMProcessor : BaseProcessor {

        private int _pageSize;
        private BOMRepository _bomRepository;

        public BOMProcessor(int pageSize) {
            this._pageSize = pageSize;
            this._bomRepository = new BOMRepository(pageSize);
        }

        public BOM FetchBOMById(int bomId) {
            try {
                return this._bomRepository.FetchBOMById(bomId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定 BOM 失败！");
            }
        }

        public List<BOMDetailsViewModel> FetchBOMDetailsViewModelList(int bomId) {
            try {
                return this._bomRepository.FetchBOMByIdForView(bomId).BOMDetails.Select(x => {
                    return new BOMDetailsViewModel() {
                        Key = x.BOMDetailsId,
                        MaterialCode = x.Material.Code,
                        MaterialName = x.Material.Name,
                        MaterialDescription = x.Material.Remarks,
                        MaterialSource = x.Basic.Code,
                        SingleCosts = x.SingleCosts,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime,
                        ModifiedBy = x.ModifiedBy,
                        ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定 BOM 明细失败！");
            }
        }

        public List<BOMViewModel> FetchBOMViewModelList(BOMSearchModel searchModel, int pageIndex = 1) {
            try {
                List<BOM> resultList = this._bomRepository.FetchBOMList(searchModel, pageIndex);
                return resultList.Select(x => {
                    return new BOMViewModel() {
                        Key = x.BOMId,
                        Code = x.Code,
                        Description = x.Description,
                        IsEnabled = x.IsEnabled,
                        BOMStatus = MethodHelper.GetEnumsDescription(typeof(AuditStatus), x.BOMStatus),
                        AuditedBy = x.AuditedBy,
                        AuditedDatetime = x.AuditedDatetime,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime,
                        ModifiedBy = x.ModifiedBy,
                        ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取 BOM 列表失败！");
            }
        }

        public void Insert(BOM bom) {
            try {
                bom.BOMId = Guid.NewGuid().GetHashCode();
                bom.CreatedDatetime = DateTime.Now;
                bom.BOMStatus = (int)AuditStatus.Waiting;
                bom.BOMDetails.ToList().ForEach(x => {
                    x.CreatedBy = bom.CreatedBy;
                    x.CreatedDatetime = bom.CreatedDatetime;
                    x.BOMDetailsId = Guid.NewGuid().GetHashCode();
                });
                this._bomRepository.Insert(bom);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("插入新 BOM 失败！");
            }
        }

        public void Update(BOM bom) {
            try {
                BOM oldBOM = this.FetchBOMById(bom.BOMId);
                if (!oldBOM.RowVersion.SequenceEqual(bom.RowVersion)) {
                    throw this.BuildException("更新 BOM 失败，数据不一致性错误！");
                }
                bom.ModifiedDatetime = DateTime.Now;
                this._bomRepository.UpdateBOM(bom, oldBOM);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("更新 BOM 失败！");
            }
        }

        public void Delete(BOM bom) {
            try {
                this._bomRepository.Delete(bom);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除 BOM 失败！");
            }
        }

        public void Audit(BOM bom) {
            try {
                BOM oldBOM = this.FetchBOMById(bom.BOMId);
                if (!oldBOM.RowVersion.SequenceEqual(bom.RowVersion)) {
                    throw this.BuildException("审核 BOM 失败，数据不一致性错误！");
                }
                bom.AuditedDatetime = DateTime.Now;
                this._bomRepository.UpdateBOM(bom, oldBOM);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("审核 BOM 失败！");
            }
        }

        public void Delete(params int[] bomIds) {
            try {
                this._bomRepository.DeleteMultipleBOM(bomIds);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除 BOM 失败！");
            }
        }
    }
}
